<?php

namespace app\services;

use \Firebase\JWT\JWT;
use think\facade\Log;

class JwtService 
{
    private static $key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";

    public static function setJWT($time) {
        $token = array(
            // "iss"  => "http://example.org", // 签发者
            // "aud"  => "http://example.com", // 认证者
            'iat'  => time(), // 签发时间
            'nbf'  => time(), // 生效时间
            'exp'  => $time, 
            'data' => '',
        );
        return JWT::encode($token, self::$key, 'HS256');
    }

    public static function getJWT($token) {
        $data = null;
        try {
            $jwt_data = JWT::decode($token, self::$key, array('HS256'));
            $data     = (array) ($jwt_data->data);
        } catch (\Throwable $e) {
            Log::write($e->getMessage(), 'error');
            return null;
        }
        return $data;
    }
}